package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysUserFeedback;
import com.mars.module.admin.service.ISysDictDataService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysFoodRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysFoodMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysFood;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysFoodService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品业务层处理
 *
 * @author mars
 * @date 2024-04-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysFoodServiceImpl implements ISysFoodService {

    private final SysFoodMapper baseMapper;
    private final ISysDictDataService dataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysFood add(SysFoodRequest request) {
        SysFood entity = SysFood.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysFoodRequest request) {
        SysFood entity = SysFood.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysFood getById(Long id) {
        SysFood sysFood = baseMapper.selectById(id);
        sysFood.setFoodTypeName(dataService.getDictDataById("food_type", sysFood.getFoodType().toString()));
        return sysFood;
    }

    @Override
    public PageInfo<SysFood> pageList(SysFoodRequest request) {
        Page<SysFood> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysFood> query = this.buildWrapper(request);
        IPage<SysFood> pageRecord = baseMapper.selectPage(page, query);
        List<SysFood> records = pageRecord.getRecords();
        List<SysFood> recordsNew = new ArrayList<>();
        for (SysFood food : records) {
            food.setFoodTypeName(dataService.getDictDataById("food_type", food.getFoodType().toString()));
            recordsNew.add(food);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysFood> list(SysFoodRequest request) {
        LambdaQueryWrapper<SysFood> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysFood> buildWrapper(SysFoodRequest param) {
        LambdaQueryWrapper<SysFood> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysFood::getName, param.getName());
        }
        if (param.getFoodType() != null) {
            query.like(SysFood::getFoodType, param.getFoodType());
        }
        if (StringUtils.isNotBlank(param.getPicture())) {
            query.like(SysFood::getPicture, param.getPicture());
        }
        if (StringUtils.isNotBlank(param.getWeight())) {
            query.like(SysFood::getWeight, param.getWeight());
        }
        return query;
    }

}
